import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_18: 桌面播放器收藏功能正常')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001043378?url_cache_key=17e362af8b12add5e45c5e468a11c15a')
class TestCaseMusic18:
    """
    预期结果: 列表展示从最新桌面播放器收藏的内容
    """

    def setup_class(self):
        # XiaoMaUtils.clear_data("music")
        # XiaoMaUtils.clear_data("launcher")

        self.driver = init_driver()
        # 实例化页面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()

        self.music_choice_page = CommonPage(self.driver).get_music_choice_page()
        self.music_index_page = CommonPage(self.driver).get_music_index_page()
        self.music_player_page = CommonPage(self.driver).get_music_player_page()
        self.music_my_collection_page = CommonPage(self.driver).get_music_my_collection_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('music')
        self.driver.quit()

    @allure.story("step1: 进入音乐首页, 播放音乐")
    @pytest.mark.run(order=181)
    def test_music_18_1_enter_music_page(self):
        try:
            # 进入音乐
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MUSIC)
            if self.music_choice_page.is_display_title():
                self.music_choice_page.click_skip()

            self.music_index_page.click_list_music(2)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step2: 返回桌面, 收藏音乐")
    @pytest.mark.run(order=182)
    def test_music_18_2_collect_launcher_music(self):
        try:
            # 返回桌面
            self.music_index_page.click_home()
            time.sleep(15)

            # 暂停播放
            self.launcher_index_page.click_play()

            self.launcher_index_page.screenshot_part(element=None, img_name="case18_collection1",
                                                     bounds=(135, 576, 299, 720))
            self.launcher_index_page.click_collection()
            XiaoMaUtils.get_screencap("case18_collection_toast.png")
            self.launcher_index_page.screenshot_part(element=None, img_name="case18_collection2",
                                                     bounds=(135, 576, 299, 720))
            contrast_res = self.launcher_index_page.image_contrast("case18_collection1.png", "case18_collection2.png")

            assert contrast_res > 0
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)
            file1 = open('Image/case18_collection1.png', 'rb').read()
            allure.attach(file1, '未收藏时的icon', allure.attachment_type.PNG)
            file2 = open('Image/case18_collection_toast.png', 'rb').read()
            allure.attach(file2, '收藏的toast', allure.attachment_type.PNG)
            file3 = open('Image/case18_collection2.png', 'rb').read()
            allure.attach(file3, '收藏时的icon', allure.attachment_type.PNG)

    @allure.story("step3: 进入音乐收藏页, 查看收藏页面是否有数据")
    @pytest.mark.run(order=183)
    def test_music_18_3_check_collection(self):
        try:
            # 从桌面启动音乐
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MUSIC)

            self.music_index_page.click_my_music()
            assert self.music_my_collection_page.get_collection_list_music_cover(1)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


if __name__ == '__main__':
    pass
